import { k } from "./K";

/*
 * @Author: LCL 
 * @Date: 2018-07-23 20:45:21
 * @Desc: 音频管理者
 */


// export enum KingAudioStatus {
//     open = "true",
//     close = "false"
// }

// export enum KingAudioStatus {
//     open = true,
//     close = false
// }

export class KingAudioManager {
    musicVolumeKey:string = "musicVolumeKey";
    musicStatusKey:string = "musicStatusKey";
    soundVolumeKey:string = "soundVolumeKey";
    soundStatusKey:string = "soundStatusKey";

    musicVolume = 1;       //音乐竟量大小
    musicStatus = true;         //音乐开还是关

    soundVolume = 1;           // 音效音量大小
    soundStatus = true;             //音效开关

    bgAudioID = -1;            //   背景 音乐  id
    yinXiaoMap;
    yinyueMap;
    audioMap;

    constructor(){    
        this.yinXiaoMap = new Map();
        this.yinyueMap = new Map();
        this.audioMap = new Map(); // 所有音频 名称：路径 对应表
        this.musicVolume = parseFloat(k.storage.getItem(this.musicVolumeKey, false)) || 1;
        this.musicStatus = k.storage.getItem(this.musicStatusKey,false);
        this.soundVolume = parseFloat(k.storage.getItem(this.soundVolumeKey, false)) || 1;
        this.soundStatus = k.storage.getItem(this.soundStatusKey, false) || 1;;

        // cc.game.on(cc.game.EVENT_HIDE, function () {
        //     cc.audioEngine.pauseAll();
        // });
        // cc.game.on(cc.game.EVENT_SHOW, function () {
        //     cc.audioEngine.resumeAll();
        // });

        
    }


    loadAudio(pathDatas:any[], tag:string = null){
        // tag = tag || HallController.getCurGameName();
        this.audioMap[tag] = this.audioMap[tag] || {}
        for(let i in pathDatas){
            let pathData = pathDatas[i]
            this.audioMap[tag][pathData.name] = pathData;
        }
    }
    
    playBGByName(name:string, tag:string = null){
        let url = this.getUrl(name, tag);
        this.stopBGM();
        if(url){
            if (this.musicStatus ) {
                // this.bgAudioID = cc.audioEngine.play(url, true, this.musicVolume);
                cc.loader.loadRes(url, cc.AudioClip, function (err, clip) {
                    k.audioManager.bgAudioID = cc.audioEngine.play(clip, true, k.audioManager.musicVolume);
                });
            
                
            } else {
                // this.bgAudioID = cc.audioEngine.play(url, true, 0);
                cc.loader.loadRes(url, cc.AudioClip, function (err, clip) {
                    k.audioManager.bgAudioID = cc.audioEngine.play(clip, true,  k.audioManager.musicVolume);
                });
                cc.audioEngine.pauseAll();
            }
        }
    }

    playSoundByName(name:string, loop:boolean = false, tag:string = null){
        let url = this.getUrl(name, tag);
        if (this.soundStatus) {
            loop = loop || false;
            if(this.yinXiaoMap.get(url) != null && loop){
            }else{
                cc.loader.loadRes(url, cc.AudioClip, function (err, clip) {
                    var audioId = cc.audioEngine.play(clip, loop, k.audioManager.soundVolume);
                    //k.log.debug("play url=" + url + " id=" + audioId);
                    k.audioManager.yinXiaoMap.set(url, audioId);
                });
                // var audioId = cc.audioEngine.play(url, loop, this.soundVolume);
                // k.log.debug("play url=" + url + " id=" + audioId);
                // this.yinXiaoMap.set(url, audioId);
            }
        }
    }

    getAudioId(name): any {
        let url = this.getUrl(name, null);
        if (this.yinXiaoMap.get(url) != null) {
            return this.yinXiaoMap.get(url);
        }
        return null;
    }

    private getUrl(name:string, tag:string = null) {
        name = name.replace(".mp3","");
        return "Sound/"+name;
        // return cc.url.raw("resources/Sound/" + name);
    }

    stopBGM() {
        if (this.bgAudioID >= 0) {
            cc.audioEngine.stop(this.bgAudioID);
        }
    }

    stopSound(name) {
        let url = this.getUrl(name, null);
        let auidoId = this.yinXiaoMap.get(url);
        // cc.log("stop url=" + url + " id=" + auidoId);
        if(auidoId >= 0){
            cc.audioEngine.stop(auidoId);
            this.yinXiaoMap.delete(url);
        }
    }
    //保存音效大小
    setSoundVolume(v){
        if(this.soundVolume != v){
            k.storage.setItem(this.soundVolumeKey, v, false);
            this.soundVolume = v;
        }
    }

    //保存音乐大小
    setMusicVolume(v){
        if(this.musicVolume != v) {
            k.storage.setItem(this.musicVolumeKey, v, false);
            this.musicVolume = v;
        }
    }

    //保存音乐开关状态
    setMusicStatus(v) {
        // v = String(v)
        // if(v == KingAudioStatus.close){
        //     this.pauseMusic();
        // }else{
        //     this.resumeMusic();
        // }
        // k.storage.setItem(this.musicStatusKey, v, false);
        // this.musicStatus = v;
    }

    setSoundStatus(v) {
        v = String(v)
        k.storage.setItem(this.soundStatusKey, v, false);
        this.soundStatus = v;
    }

    getState(){
        return cc.audioEngine.getState(this.bgAudioID);
    }

    //设置音效大小
    setSFXSwitch(v){
        this.soundStatus = v;
    }


    //设置音效大小
    setSFXNum(v){
        this.soundVolume = v;
    }

    pauseAll(){
        cc.audioEngine.pauseAll();
    }

    pauseMusic(){
        cc.audioEngine.pause(this.bgAudioID);
    }

    resumeAll(){
        cc.audioEngine.resumeAll();
    }

    resumeMusic(){
        cc.audioEngine.setVolume(this.bgAudioID, this.getMusicVolume());
        cc.audioEngine.resume(this.bgAudioID);
    }

    getMusicVolume() {
        return this.musicVolume;
    }

    getSoundVolume() {
        return this.soundVolume;
    }

    // getMusicStatus():KingAudioStatus {
    //     return this.musicStatus;
    // }

    // getSoundStatus():KingAudioStatus {
    //     return this.soundStatus;
    // }
}


/*
public void TEXT1() 
{
	int textNum = 1;
	if (textNum == 1) 
	{
		textNum = 2;
    }
}
public void TEXT2()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}
public void TEXT3()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT4()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT5()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT6()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT8()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT9()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT10()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT11()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT12()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT13()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT14()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT15()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT16()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT17()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}
*/